// pages/myCollection/myCollection.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    imageUrl: app.globalData.imageUrl,
    userInfo: '',
    shopList: '',
    showSelect: false,
    current:0,
    phone:'',
    tabData: [
      { txt: '房源', current: true },
      { txt: '二手', current: false },
      { txt: '职位', current: false },
      { txt: '简历', current: false },
      { txt: '店铺', current: false }
    ],
    showSelect: false,
    actiItem0: [],
    actiItem1: [],
    actiItem2: [],
    actiItem3: [],
    actiItem4: [],
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData1: true,
    hasData2: true,
    hasData0: true,
    hasData3: true,
    hasData4: true,
    page1: 1,
    page2: 1,
    page0: 1,
    page3: 1,
    page4: 1,
    current: 0,
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('userInfo') != ''){
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync('userInfo'))
      })
    }
    this.getCollectDp(true);
    this.getCollectZw(true);
    this.getCollectJl(true);
    this.getCollectEs(true);
    this.getCollectFy(true);
  },
  // 查询我的收藏房源数据
  getCollectFy: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyHouseRental/collection/byUserId',
      data: {
        currentPage: this.data.page0,
        pageNumber: '10',
        userId: this.data.userInfo.id
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data.pp) {
            if (res.data.data.pp.length < 10) {
              _this.setData({
                hasData0: false
              })
            } else {
              _this.setData({
                hasData0: true
              })
            }
            res.data.data.pp.forEach(function (ele) {
              if (ele.picture) {
                ele.picture = ele.picture.split(",");
              }
              if (ele.housingCharacteristics) {
                ele.housingCharacteristics = ele.housingCharacteristics.split(",");
              }
              if (ele.housingDescription && ele.housingDescription.length > 70) {
                ele.housingDescription = ele.housingDescription.substring(0, 70) + '...'
              }
            })
            if (flag) {
              wx.hideNavigationBarLoading() //完成停止加载
              wx.stopPullDownRefresh() //停止下拉刷新
              _this.setData({
                actiItem0: []
              });
            } else {
              _this.setData({
                loadMore: false
              });
            }
            _this.data.actiItem0 = _this.data.actiItem0.concat(res.data.data.pp);
            _this.setData({
              actiItem0: _this.data.actiItem0
            })
          }
        }
      }
    })
  },
  // 查询我的收藏二手数据
  getCollectEs: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyCollection',
      data: {
        currentPage: this.data.page1,
        pageNumber: '10',
        userId: this.data.userInfo.id,
        block: 4
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data.secondHandInfos) {
          if (res.data.data.secondHandInfos.length < 10) {
            _this.setData({
              hasData1: false
            })
          } else {
            _this.setData({
              hasData1: true
            })
          }
          res.data.data.secondHandInfos.forEach(function (ele) {
            if (ele.backupA) {
              ele.backupA = ele.backupA.split(",");
            }
            if (ele.createTime) {
              ele.createTime = util.getDays(ele.createTime)
            }
            if (ele.describe) {
              if (ele.describe.length > 70) {
                ele.describe = ele.describe.substring(0, 70) + "...";
              }
            }
          })
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            _this.setData({
              actiItem1: []
            });
          } else {
            _this.setData({
              loadMore: false
            });
          }
          _this.data.actiItem1 = _this.data.actiItem1.concat(res.data.data.secondHandInfos);
          _this.setData({
            actiItem1: _this.data.actiItem1
          })
          }
        }
      }
    })
  },
  // 查询我的收藏店铺数据
  getCollectDp: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyCollection',
      data: {
        currentPage: this.data.page4,
        pageNumber: '10',
        userId: this.data.userInfo.id,
        block:1
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.shopInfos){
          if (res.data.data.shopInfos.length < 10) {
          _this.setData({
            hasData4: false
          })
        } else {
          _this.setData({
            hasData4: true
          })
        }
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem4: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.actiItem4 = _this.data.actiItem4.concat(res.data.data.shopInfos);
        _this.setData({
          actiItem4: _this.data.actiItem4
        })
        }
      }
    })
  },
  // 查询我的收藏职位数据
  getCollectZw: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyCollection',
      data: {
        currentPage: this.data.page2,
        pageNumber: '10',
        userId: this.data.userInfo.id,
        block: 5
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.positionInfos){
        if (res.data.data.positionInfos.length < 10) {
          _this.setData({
            hasData2: false
          })
        } else {
          _this.setData({
            hasData2: true
          })
        }
        res.data.data.positionInfos.forEach(function (ele) {
          if (ele.createTime) {
            ele.time = util.formatDate(ele.createTime);
          }
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            actiItem2: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.actiItem2 = _this.data.actiItem2.concat(res.data.data.positionInfos);
        _this.setData({
          actiItem2: _this.data.actiItem2
        })
        }
      }
    })
  },
  // 查询我的收藏简历数据
  getCollectJl: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'personalCenter/getMyCollection',
      data: {
        currentPage: this.data.page3,
        pageNumber: '10',
        userId: this.data.userInfo.id,
        block: 6
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if(res.data.success){
          if (res.data.data.personalInfos){
          if (res.data.data.personalInfos.length < 10) {
            _this.setData({
              hasData3: false
            })
          } else {
            _this.setData({
              hasData3: true
            })
          }
          res.data.data.personalInfos.forEach(function (ele,index) {
            if(ele){
              if (ele.enterWorkDate) {
                var year = new Date(ele.enterWorkDate.replace(/\-/g, "/"));
                var now = new Date();
                var workYear = parseInt(now - year) / 1000 / 3600 / 24 / 365;
                ele.workYear = workYear.toFixed(0);
              }
            }else{
              res.data.data.personalInfos.splice(index,1);
            }
          })
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            _this.setData({
              actiItem3: []
            });
          } else {
            _this.setData({
              loadMore: false
            });
          }
          _this.data.actiItem3 = _this.data.actiItem3.concat(res.data.data.personalInfos);
          _this.setData({
            actiItem3: _this.data.actiItem3
          })
          }
        }
      }
    })
  },
  swichTab: function (e) {
    this.data.tabData.forEach(function (ele) {
      ele.current = false;
    })
    this.data.tabData[e.currentTarget.dataset.value].current = true;
    this.setData({
      tabData: this.data.tabData,
      current: parseInt(e.currentTarget.dataset.value)
    })
  },
  // 商家详情
  showShopIndex: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/shopIndex/shopIndex?id='+id,
    })
  },
  // 弹出打电话
  callShop: function (e) {
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show,
      phone: e.currentTarget.dataset.tel
    })
  },
  // 调用打电话
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value //仅为示例，并非真实的电话号码
    })
    this.setData({
      showSelect: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
        loadSuccess: false
      })
      wx.showLoading();
      this.setData({
        ['page' + this.data.current]: 1
      })
      if (this.data.current == 4) {
        this.getCollectDp(true);
      } else if (this.data.current == 2) {
        this.getCollectZw(true);
      } else if (this.data.current == 3) {
        this.getCollectJl(true);
      } else if (this.data.current == 1) {
        this.getCollectEs(true);
      }else{
        this.getCollectFy(true);
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      ['page' + this.data.current]: 1
    })
    if (this.data.current == 4) {
      this.getCollectDp(true);
    } else if (this.data.current == 2) {
      this.getCollectZw(true);
    } else if (this.data.current == 3) {
      this.getCollectJl(true);
    } else if (this.data.current == 1) {
      this.getCollectEs(true);
    } else {
      this.getCollectFy(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data['hasData' + this.data.current]) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        ['page' + this.data.current]: this.data['page' + this.data.current] + 1
      });
      if (this.data.current == 4) {
        this.getCollectDp();
      } else if (this.data.current == 2) {
        this.getCollectZw();
      } else if (this.data.current == 3) {
        this.getCollectJl();
      } else if (this.data.current == 1) {
        this.getCollectEs();
      } else {
        this.getCollectFy();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})